﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure.UI.Abstract;
using System.Windows.Forms;
using System.Reflection;

namespace Infrastructure.UI
{
    /// <summary>
    /// BindingManager 实现绑定管理
    /// </summary>
    public class BindingManager : AbstractBindingManager
    {
        public BindingManager()
        {

        }
        private static BindingManager _instance;
        public static BindingManager Instance
        {
            get { return _instance ?? (_instance = new BindingManager()); }
        }
        /// <summary>
        /// Actuals the binding.
        /// </summary>
        /// <param name="control">The control.</param>
        public override bool ActualBinding(Control control, PropertyInfo property, Object obj)
        {
            bool hasDone = false;

            #region Windows Form Controls
            if (control is TextBox)
            {
                hasDone = Binding((TextBox)control, property, obj);
            }
            else if (control is ComboBox)
            {
                hasDone = Binding((ComboBox)control, property, obj);
            }
            else if (control is CheckBox)
            {
                hasDone = Binding((CheckBox)control, property, obj);
            }
            else if (control is RadioButton)
            {
                hasDone = Binding((RadioButton)control, property, obj);
            }
            else if (control is DateTimePicker)
            {
                hasDone = Binding((DateTimePicker)control, property, obj);
            }
            #endregion

            return hasDone;
        }


        #region Bingdings

        private bool Binding(TextBox control, PropertyInfo property, object obj)
        {
            control.Text = null;
            if (obj == null) return true;
            DoBinding(control, "Text", obj, property);
            return true;
        }

        private bool Binding(ComboBox control, PropertyInfo property, object obj)
        {
            control.Text = null;
            if (obj == null) return true;
            DoBinding(control, "Text", obj, property);
            return true;
        }

        private bool Binding(CheckBox control, PropertyInfo property, object obj)
        {
            control.Checked = false;
            if (obj == null) return true;
            DoBinding(control, "Checked", obj, property);
            return true;
        }

        private bool Binding(RadioButton control, PropertyInfo property, object obj)
        {
            control.Checked = false;
            if (obj == null) return true;
            DoBinding(control, "Checked", obj, property);
            return true;
        }

        private bool Binding(DateTimePicker control, PropertyInfo property, object obj)
        {
            control.Value = control.MinDate;
            if (obj == null) return true;
            DoBinding(control, "Value", obj, property);
            return true;
        }
        #endregion
    }
}
